package cn.kgc.controller.manage;


import cn.kgc.pojo.*;
import cn.kgc.service.system.jjl_manageService;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.net.URLEncoder;
import jdk.nashorn.internal.objects.NativeError;
import org.apache.commons.collections4.iterators.ListIteratorWrapper;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.yaml.snakeyaml.events.Event;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manageA")
public class jjl_manageController {



    @RequestMapping("jjl")
    public String see( Model model) {
        return "jjl_Manage";
    }

    @RequestMapping("jjl_dpinsert")
    public  String looklook(Model model){

        List<Department> list =ms.jjl_dp();
        model.addAttribute("dplist",list);
        System.out.println(list.toString());
        return "jjl_dp";}

    @Resource
    private jjl_manageService ms;


    /*前端页面查询基本信息*/

    @RequestMapping("/jjl_sel")
    public String jjl_sel(Model model,
                          @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(required = false, defaultValue = "") String chaozhao,
                          @RequestParam(required = false, defaultValue = "0",value ="bumen")  int bumen,
                          @RequestParam(required = false, defaultValue = "0") int zhiwei) {

        PageHelper.startPage(pageNum,10);
        Page<User> list =ms.jjl_page(chaozhao,bumen,zhiwei);
        List<Jobtitle> bjob=ms.jjl_bjob();
        List<Department> dop = ms.jjl_dp();

        PageInfo<User> page = new PageInfo<User>(list);


        model.addAttribute("listA", page);
        model.addAttribute("dop",dop);
        model.addAttribute("bbba",bjob);


        return "jjl_Manage";


    }

    /*删除操作*/
    @RequestMapping("/jjl_del")
    public boolean jjl_del(Integer id) {
        if (ms.jjl_deleteById(id) == 1) {
            return true;
        } else {
            return false;
        }
    }

    /*详细修改*/
    @RequestMapping("jjl_update")
    @ResponseBody
    public boolean jjl_update( Integer id,
                            Integer jt_id,Integer state) {


        if(jt_id!=null){
            ms.updatejob(id,jt_id,state);
            return true;
        }

        return true;

    }

    @GetMapping("jjl_byid")
    public String jjl(Model model, @RequestParam(required = false, value = "yonghuid") Integer yonghuid) {

        User user = new User();

        user = ms.jjl_queryById(yonghuid);
        ;


        model.addAttribute("user", user);
        List<Department> dop = ms.jjl_dp();
        List<Jobtitle> job = ms.jjl_job(user.getJobtitle().getDp_id());
        model.addAttribute("joba", job);
        model.addAttribute("dop", dop);
        model.addAttribute("yongid", yonghuid);

        return "jjl_tianjia";
    }



    @RequestMapping("jjl_chaxun")
    @ResponseBody
    public List<Jobtitle> jjl_chaxun(  Integer dp_id) {
        List<Jobtitle> job = ms.jjl_job(dp_id);
        job.forEach(System.out::println);

        return job;
    }
    @ResponseBody
    @RequestMapping("jjl_insert")
    public  boolean jjl_insert(User user){

       if( ms.jjl_insert(user)>0){

           return  true;
       }

        return  false;
    }
    @RequestMapping("jjl_insertdp")
    @ResponseBody
    public  boolean jjl_insertdp(String departmentName){
         if (ms.insertdp(departmentName)){
             return  true;
         }
         return false;
    }
    @RequestMapping("/download")
    public void download(HttpServletResponse response) throws Exception {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("员工用户表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");
        List<jjl_excal> list = ms.jjl_excel();
        System.out.println(list.toString());

        EasyExcel.write(response.getOutputStream(), jjl_excal.class).sheet("第一个sheet").doWrite(list);;
    }

    @RequestMapping("jjl_listjob")
    public ModelAndView listjob(Jobtitle jb){
        ModelAndView mv = new ModelAndView();

        List<Jobtitle> jobtitles = ms.jjl_job(jb.getId());
        mv.addObject("jobtitles",jobtitles);
        mv.addObject("jobtitless",jb);
        mv.setViewName("jjl_job");
        System.out.println(jobtitles.toString());
       return mv;
    }

    @ResponseBody
  @RequestMapping("jjl_tianjiajob")
    public boolean tianjiajob(Integer dp_id,String job){
      boolean bo = ms.insertjob(dp_id,job);
        return  bo;
  }

  @RequestMapping("jjl_deldp")
    @ResponseBody
    public boolean deldp( int id){
        boolean bo =ms.deldp(id);
      return  bo;
  }

  @RequestMapping("jjl_bydpid")
  @ResponseBody
    public  String  jjl_bydpid( int typeId){
        List<Jobtitle> list =ms.jjl_job(typeId);
      Map rstMap = new HashMap<>();
      //返回格式符合LayUi的要求
      rstMap.put("code",0);
      rstMap.put("msg","");
      rstMap.put("count",list != null ? list.size() : 0);
      rstMap.put("data",list);

      System.out.println(list.toString()+"+++++++++++++++++++++++++++++++++++++++++++++++++++++++");
      return JSON.toJSONString(rstMap);

  }

    @ResponseBody
    @RequestMapping("jjl_delbyidjob")
    public  boolean jjl_delbyidjob(Integer jobid){
        System.out.println("进来了+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
      return   ms.deljob(jobid);
    }

}

